package com.chenqq.qsocket.handler;

import com.chenqq.qsocket.listener.SocketMessageListener;
import com.chenqq.qsocket.message.SocketMessage;
import com.chenqq.qsocket.message.TextMessage;

import org.apache.mina.core.session.IoSession;
import org.json.JSONException;

import java.net.SocketAddress;

public class MessageInfoHandler implements MessageHandler{
    private SocketMessageListener listener;
    @Override
    public void handleMessage(SocketMessage msg, IoSession session) throws JSONException {
        TextMessage textMessage = new TextMessage(msg);
        TextMessage.MessageInfo messageInfo = textMessage.getMessageInfo();
        SocketAddress remoteAddress = session.getRemoteAddress();
        String replace = remoteAddress.toString().replace("/", "");
        String[] split = replace.split(":");
        if (listener!=null){
            listener.receiveMessageSuccess(split[0],messageInfo);
        }
    }

    @Override
    public void handleListener(String tag, SocketMessageListener listener) {
        this.listener=listener;
    }
}
